package com.bjqg.bjqg01.bjqg.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqg.bjqg01.bjqg.R;
import com.bjqg.bjqg01.bjqg.bean.UserLoginBean;
import com.bjqg.bjqg01.bjqg.utils.Constant;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import java.net.URLEncoder;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by bjqg01 on 2017/12/11.
 */
public class ForgetPawActivity extends BaseActivity implements View.OnClickListener {

    private TextView phonenum;
    private TextView yznum;
    private TextView getyznum;
    private TextView pwd1;
    private TextView pwd2;
    private TextView tvsend;
    private ImageView back;
    private TextView toptitle;
    private ImageView select;
    private String getPhoneNum;
    private String getYz;
    private String getPwd1;
    private String getPwd2;
    private String[] split1;
    private boolean isrun = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
//        setContentView(R.layout.activity_forget_password);

        initView();
    }

    private void initView() {
        phonenum = (TextView) findViewById(R.id.wj_phone_num);//手机号
        yznum = (TextView) findViewById(R.id.wj_yz_num);//验证码
        getyznum = (TextView) findViewById(R.id.wj_get_yz_num);//获取验证码
        pwd1 = (TextView) findViewById(R.id.wj_pwd1);//密码1
        pwd2 = (TextView) findViewById(R.id.wj_pwd2);//密码2
        tvsend = (TextView) findViewById(R.id.wj_send);//提交

        phonenum.setText("18630939453");
        yznum.setText("0000");
        pwd1.setText("654321");
        pwd2.setText("654321");

        back = (ImageView) findViewById(R.id.back);//返回
        toptitle = (TextView) findViewById(R.id.tv_top_title);//标题
        select = (ImageView) findViewById(R.id.select);

        getyznum.setOnClickListener(this);
        tvsend.setOnClickListener(this);
        back.setOnClickListener(this);

        toptitle.setText("忘记密码");
        select.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.wj_send:
//                Toast.makeText(getApplicationContext(),"提交",Toast.LENGTH_SHORT).show();
                getText();
                getTextNotNull();

                if (!checkCellphone(getPhoneNum)){
                    Toast.makeText(getApplicationContext(),"手机号不合法",Toast.LENGTH_SHORT).show();
                    return;
                }
                if (isrun){
//                    loadview.setVisibility(View.GONE);
                    isrun = false;
                    return;
                }
//                getokgo(getUrls());
//                finish();
                break;
            case R.id.wj_get_yz_num:
                getPhoneNum = phonenum.getText().toString().trim();

                if (!checkCellphone(getPhoneNum)){
                    Toast.makeText(getApplicationContext(),"手机号不合法",Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(ForgetPawActivity.this,"获取验证码",Toast.LENGTH_SHORT).show();
                break;
        }
    }

    private Handler myHandler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
//                    loadview.setVisibility(View.GONE);
                    Intent intent = new Intent();
                    intent.setClass(ForgetPawActivity.this, ForgetPwdSuccessActivity.class);
                    startActivity(intent);
                    finish();
                    break;
                case 1:
                    UserLoginBean refreshinfo = msg.getData().getParcelable("userLoginInfo");
                    List<UserLoginBean.DsBean> ds = refreshinfo.getDs();
                    if (getPwd1.equals(ds.get(0).getPwd())){
//                        loadview.setVisibility(View.GONE);
                        Toast.makeText(ForgetPawActivity.this, "新密码与原密码相同，请重新输入", Toast.LENGTH_LONG).show();
                        isrun=true;
                        return;
                    }
//                    getokgo(getUrl());
                    break;
            }

        }
    };

    private void getokgo(String url) {

//        OkGo.<String>get(url)
////        OkGo.<String>get("http://www.tjzj21erp.com:8098/Service1.asmx/SelectjycsDj?yema="+yema+"&sqlwhere=")
//                .tag(this)
//                .execute(new StringCallback() {
//                    @Override
//                    public void onSuccess(com.lzy.okgo.model.Response<String> response) {
////                        if (sampleInfoBean.getDs().size()==0){
////                            int i=0;
//                        Log.e("----------error","失败：------------------------okgo");
////                        }
//                        String str= "<string xmlns=\"http://tempuri.org/\">";
//                        String strend="</string>";
//                        String strerror = "{\"ds\":{}}";
//                        String[] split = response.body().toString().split(str);
////                        Log.e("onSuccess: ", ""+split[1]);
//                        split1 = split[1].split(strend);
////                        if (split1[0].contains("\\")){
////                            split1[0] = split1[0].replace("\\", "/");
////                        }
//                        Log.e("----------error","失败：------------------------okgo"+ split1[0]);
////                        strreturn[0] = split1[0];
//                        if (strerror.equals(split1[0])){
//                            Toast.makeText(ForgetPawActivity.this,"服务器正忙，请稍后再试!",Toast.LENGTH_SHORT).show();
////                            loadview.setVisibility(View.GONE);
////                            username.setText("");
////                            userpwd.setText("");
//                        }else if(split1[0].equals("修改密码成功")){
////                            Gson gson = new Gson();
////                            UserLoginBean userLoginInfo = gson.fromJson(split1[0], UserLoginBean.class);
//                            Message message = Message.obtain();
//                            Bundle bundle = new Bundle();
////                            bundle.putParcelable("userLoginInfo", userLoginInfo);
//                            message.what = 0;
//                            message.setData(bundle);
//                            myHandler.sendMessage(message);
//                        }else {
//                            Gson gson = new Gson();
//                            UserLoginBean userLoginInfo = gson.fromJson(split1[0], UserLoginBean.class);
//
//                            Message message = Message.obtain();
//                            Bundle bundle = new Bundle();
//                            bundle.putParcelable("userLoginInfo", userLoginInfo);
//                            message.what = 1;
//                            message.setData(bundle);
//                            myHandler.sendMessage(message);
//                        }
//                    }
//
//                    @Override
//                    public void onError(com.lzy.okgo.model.Response<String> response) {
//                        super.onError(response);
////                        Log.e("----------error","失败：------------------------okgo");
//                        Toast.makeText(ForgetPawActivity.this,"服务器正忙，请稍后再试!",Toast.LENGTH_SHORT).show();
////                        loadview.setVisibility(View.GONE);
//                    }
//                });

    }

    private String getUrls() {
        StringBuffer sb = new StringBuffer();
        getPhoneNum = phonenum.getText().toString().trim();
        if (getPhoneNum!=null&&getPhoneNum!=""&&!getPhoneNum.equals("")){
            String usernamecode = URLEncoder.encode(getPhoneNum);
            sb.append("phonenum="+usernamecode);
        }
//        if (getuserpwd!=null&&getuserpwd!=""&&!getuserpwd.equals("")){
//            if (sb.toString().length()!=0)sb.append("&");
//            String jyxmcode = URLEncoder.encode(getuserpwd);
//            sb.append("jyxm="+jyxmcode);
//        }
        String url = Constant.BASEURL+Constant.SELECTAPPUSER+"?" + sb.toString();
        Log.e("----------error","失败：------------------------"+url);
        return url;
    }

    @android.support.annotation.NonNull
    private String getUrl() {
        StringBuffer sb = new StringBuffer();
//        getText();
//        getTextNotNull();

        String phonecode = URLEncoder.encode(getPhoneNum);
        sb.append("phonenum="+phonecode);
        String pwdcode = URLEncoder.encode(getPwd1);
        sb.append("&pwd="+pwdcode);

        String url = Constant.BASEURL+Constant.UPDATEPWD+"?" + sb.toString();
        Log.e("----------error","失败：------------------------"+url);
        return url;
    }

    public void getTextNotNull(){
        if (getPhoneNum==null&&getPhoneNum.equals("")){
            Toast.makeText(getApplicationContext(),"手机号不能为空",Toast.LENGTH_SHORT).show();
            isrun =true;
        }else if (getYz==null&&getYz.equals("")){
            Toast.makeText(getApplicationContext(),"验证码不能为空",Toast.LENGTH_SHORT).show();
            isrun =true;
        }else if (getPwd1==null&&getPwd1.equals("")){
            Toast.makeText(getApplicationContext(),"密码不能为空",Toast.LENGTH_SHORT).show();
            isrun =true;
        }else if (getPwd2==null&&getPwd2.equals("")){
            Toast.makeText(getApplicationContext(),"密码不能为空",Toast.LENGTH_SHORT).show();
            isrun =true;
        }else if (!getPwd1.equals(getPwd2)){
            Toast.makeText(getApplicationContext(),"两次输入密码不一致",Toast.LENGTH_SHORT).show();
            isrun =true;
        }
    }

    public void getText(){
        getPhoneNum = phonenum.getText().toString().trim();
        getYz = yznum.getText().toString().trim();
        getPwd1 = pwd1.getText().toString().trim();
        getPwd2 = pwd2.getText().toString().trim();
    }

    /**
     * 验证手机号码
     *
     * 移动号码段:139、138、137、136、135、134、150、151、152、157、158、159、182、183、187、188、147、182
     * 联通号码段:130、131、132、136、185、186、145
     * 电信号码段:133、153、180、189、177
     *
     * @param cellphone
     * @return
     */
    public static boolean checkCellphone(String cellphone) {
        String regex = "^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,1,2,3,5-9])|(177))\\d{8}$";
        Pattern pattern=Pattern.compile(regex);
        Matcher matcher=pattern.matcher(cellphone);
        return matcher.matches();
    }
}
